<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          :preIcon="IconEnum.ADD"
          v-auth="OpFaultProcessAuth.ADD"
          @click="handleCreate"
          type="primary"
        >
          新增
        </a-button>
        <a-button
          :preIcon="IconEnum.DELETE"
          v-auth="OpFaultProcessAuth.DEL"
          @click="handleDelete"
          type="primary"
          color="error"
        >
          删除
        </a-button>
        <a-button
          :preIcon="IconEnum.EXPORT"
          v-auth="OpFaultProcessAuth.EXPORT"
          @click="handleExport"
          type="primary"
          color="warning"
        >
          导出
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: IconEnum.VIEW,
              tooltip: '查看',
              auth: OpFaultProcessAuth.SINGLE,
              onClick: handleView.bind(null, record),
            },
            {
              icon: IconEnum.EDIT,
              tooltip: '编辑',
              auth: OpFaultProcessAuth.EDIT,
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: IconEnum.DELETE,
              tooltip: '删除',
              auth: OpFaultProcessAuth.DEL,
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
            {
              label: '受理',
              tooltip: '受理',
              auth: OpFaultProcessAuth.DISPOSE,
              ifShow: record.faultProcessStatus == '待受理',
              onClick: handleDispose.bind(null, record),
            },
            {
              label: '完成处理',
              tooltip: '事件处理完成',
              auth: OpFaultProcessAuth.DISPOSEREJECT,
              ifShow: record.faultProcessStatus == '处理中',
              onClick: handleDispose.bind(null, record),
            },
            {
              label: '关闭工单',
              tooltip: '工单关闭',
              auth: OpFaultProcessAuth.DISPOSECLOSE,
              ifShow: record.faultProcessStatus == '待结束',
              onClick: handleDispose.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <RefreshDrawer @register="refreshRegisterDrawer" @success="handleSuccess" />
    <DetailDrawer @register="detailRegisterDrawer" />
    <AcceptedDrawer @register="acceptedRegisterDrawer" />


  </div>
</template>

<script setup lang="ts">
  import { reactive } from 'vue';
  import { listOpFaultProcessApi, delOpFaultProcessApi, exportOpFaultProcessApi } from '@/api/operations/faultprocess.api';
  import { useDrawer } from '@/components/Drawer';
  import RefreshDrawer from './Drawer.vue';
  import DetailDrawer from './Detail.vue';
  import AcceptedDrawer from './Accepted2.vue';

  import { useMessage } from '@/hooks/web/useMessage';
  import { IconEnum } from '@/enums';
  import { BasicTable, TableAction, useTable } from '@/components/Table';
  import { OpFaultProcessAuth } from '@/auth/operations/faultprocess.auth';
  import { columns, searchFormSchema } from './data';
  import { OpFaultProcessPM } from '@/model/operations/faultprocess.model';



  const { createMessage, createConfirm } = useMessage();
  const state = reactive<{
    ids: string[];
    idNames: string;
  }>({
    ids: [],
    idNames: '',
  });

  const [refreshRegisterDrawer, { openDrawer: refreshOpenDrawer }] = useDrawer();
  const [detailRegisterDrawer, { openDrawer: detailOpenDrawer }] = useDrawer();

  // 注册受理抽屉
  const [acceptedRegisterDrawer, { openDrawer: acceptedOpenDrawer }] = useDrawer();

  const [registerTable, { reload, getForm }] = useTable({
    title: '工单流程列表',
    api: listOpFaultProcessApi,
    striped: false,
    useSearchForm: true,
    rowKey: 'id',
    bordered: true,
    showIndexColumn: true,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    showTableSetting: true,
    tableSetting: {
      fullScreen: true,
    },
    actionColumn: {
      width: 220,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
    },
    rowSelection: {
      onChange: (selectedRowKeys, selectRows) => {
        state.ids = selectedRowKeys as string[];
        state.idNames = selectRows
          .map((item) => {
            return item.id;
          })
          .join(',');
      },
    },
  });



  /** 查看按钮 */
  function handleView(record: Recordable) {
    acceptedOpenDrawer(true, {
      record,
    });
  }

  /** 新增按钮 */
  function handleCreate() {
    refreshOpenDrawer(true, {
      isUpdate: false,
    });
  }

  /** 修改按钮 */
  function handleEdit(record: Recordable) {
    refreshOpenDrawer(true, {
      record,
      isUpdate: true,
    });
  }

  /** 删除按钮 */
  function handleDelete(record: Recordable) {
    const delIds = record?.id || state.ids;
    const delNames = record?.id || state.idNames;
    if (!record?.id && state.ids.length === 0) {
      createMessage.warning('请选择要操作的数据！');
    } else {
      createConfirm({
        iconType: 'warning',
        title: '提示',
        content: '是否确定要删除' + delNames + '?',
        onOk: () =>
          delOpFaultProcessApi(delIds).then(() => {
            createMessage.success('删除' + delNames + '成功！');
            reload();
          }),
      });
    }
  }

  /** 导出按钮 */
  function handleExport() {
    const value = getForm().getFieldsValue() as OpFaultProcessPM;
    exportOpFaultProcessApi(value);
  }

  /** 受理按钮 */
  function handleDispose(record: Recordable) {
    if (!record?.id) {
      createMessage.warning('请选择要操作的数据！');
    }else{
      acceptedOpenDrawer(true, {
        record,
      });
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
